package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.dto.VmTypePageDto;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.VmType;
import com.heima.service.VmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VmTypeServiceImpl extends ServiceImpl<VmTypeMapper, VmType> implements VmTypeService {

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Override
    public PageResult<VmType> search(VmTypePageDto pd) {

        Page<VmType> page = new Page<>(pd.getPageIndex(), pd.getPageSize());
        if (pd.getName()!=null && pd.getName() != ""){
            Page<VmType> pageList = vmTypeMapper.selectPage(page, Wrappers.<VmType>lambdaQuery().like(VmType::getName, pd.getName()));
            return new PageResult<VmType>(pd.getPageIndex(), pd.getPageSize(),
                    pageList.getTotal(),pageList.getPages(),
                    pageList.getRecords());
        }
        LambdaQueryWrapper<VmType> qw = new LambdaQueryWrapper<>();
        Page<VmType> list = vmTypeMapper.selectPage(page, qw);
        return new PageResult<>(pd.getPageIndex(), pd.getPageSize(),list.getPages(),list.getTotal(),list.getRecords());

    }
}
